package com.lanqiao.attendance.handler.TeacherHandler;

import com.alibaba.fastjson.JSON;
import com.lanqiao.attendance.service.TeacherService;
import com.lanqiao.attendance.utils.HttpContextUtils;
import com.lanqiao.attendance.utils.JWTUtils;
import com.lanqiao.attendance.vo.ErrorCode;
import com.lanqiao.attendance.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * * @Author Pu Yunsheng
 * * @Data 2022/7/11 上午 01:18
 * * @Version 1.0
 **/

@Aspect
@Component
public class TeacherTokenVerifyAop {

    @Resource(name = "teacherServiceImpl")
    TeacherService teacherService;

    @Around("@within(com.lanqiao.attendance.handler.TeacherHandler.TeacherTokenVerify) || @annotation(com.lanqiao.attendance.handler.TeacherHandler.TeacherTokenVerify)")
    public Result around(ProceedingJoinPoint joinPoint) throws Throwable {
        Result result = null;
        //获取request对象
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        //获取对象头中的token信息
        String token = request.getHeader("Authorization");
        //逻辑代码
        if (StringUtils.isBlank(token)){
            result = Result.fail(ErrorCode.NO_LOGIN.getCode(), ErrorCode.NO_LOGIN.getMsg());
        }
        else if(teacherService.checkToken(token)){
            result = Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }
        else {
            result = (Result) joinPoint.proceed();
        }
        return result;
    }
}
